﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Xml;

namespace Xinnos.Lca.Yacht.Analyzer.View
{
    internal class WorkBreakdownStructureGroupViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public ObservableCollection<WorkBreakdownStructureViewModel> WorkBreakdownStructures { get; set; }
        private WorkBreakdownStructureViewModel mWorkBreakdownStructure = null;
        private string mName = null;

        public WorkBreakdownStructureViewModel WorkBreakdownStructure
        {
            get { return mWorkBreakdownStructure; }
            set
            {
                mWorkBreakdownStructure = value;
                RaisePropertyChanged("WorkBreakdownStructure");
            }
        }
        public string Name
        {
            get { return mName; }
            set
            {
                mName = value;
                RaisePropertyChanged("Name");
            }
        }

        public WorkBreakdownStructureGroupViewModel()
        {
            if (null == WorkBreakdownStructures) WorkBreakdownStructures = new ObservableCollection<WorkBreakdownStructureViewModel>();
        }

        public WorkBreakdownStructureViewModel FindByCode(string strCode)
        {
            WorkBreakdownStructureViewModel _found = null;
            if (string.IsNullOrEmpty(strCode)) return _found;
            foreach (WorkBreakdownStructureViewModel _structure in this.WorkBreakdownStructures)
            {
                if (strCode == _structure.Code)
                {
                    _found = _structure;
                    break;
                }
            }
            return _found;
        }

        public XmlElement Export(XmlDocument doc)
        {
            XmlElement _element = null;
            if (null != doc)
            {
                _element = doc.CreateElement("WorkBreakdownStructureGroup");
                if (!string.IsNullOrEmpty(this.Name))
                {
                    XmlAttribute _name = doc.CreateAttribute("Name");
                    _name.Value = this.Name;
                    _element.Attributes.Append(_name);
                }
                if (0 < WorkBreakdownStructures.Count)
                {
                    XmlElement _children = doc.CreateElement("WorkBreakdownStructures");
                    foreach (WorkBreakdownStructureViewModel _structure in WorkBreakdownStructures) _children.AppendChild(_structure.Export(doc));
                    _element.AppendChild(_children);
                }
            }
            return _element;
        }

        public void Import(XmlElement element)
        {
            if (null == element) return;
            if ("WorkBreakdownStructureGroup" == element.Name)
            {
                if (element.HasAttribute("Name")) this.Name = element.Attributes["Name"].Value;
                else this.Name = null;

                WorkBreakdownStructures.Clear();
                if (element.HasChildNodes)
                {
                    foreach (XmlNode _structures in element.ChildNodes)
                    {
                        if ("WorkBreakdownStructures" == _structures.Name)
                        {
                            if (_structures.HasChildNodes)
                            {
                                ConfigurationViewModel _configuration = ConfigurationViewModel.GetInstance();
                                foreach (XmlNode _structure in _structures)
                                {
                                    WorkBreakdownStructureViewModel _wbs = null;
                                    if (_configuration.Object == this.Name) _wbs = new ObjectViewModel();
                                    else _wbs = new WorkBreakdownStructureViewModel();
                                    _wbs.Import(_structure as XmlElement);
                                    if (null == _wbs.Group) _wbs.Group = this;
                                    WorkBreakdownStructures.Add(_wbs);
                                }
                            }
                        }
                    }
                }
            }
        }

        private void RaisePropertyChanged(string strPropertyName)
        {
            PropertyChangedEventHandler _handler = this.PropertyChanged;
            if (null != _handler) _handler(this, new PropertyChangedEventArgs(strPropertyName));
        }
    }
}
